package com.hns.can.master.web;/**
 * Created by zhangyu on 2017/4/13.
 */

import com.hns.can.common.domain.GridPage;
import com.hns.can.common.web.BaseController;
import com.hns.can.master.entity.BaseCarModelInfoEntity;
import com.hns.can.master.service.BaseCarModelInfoService;
import com.hns.frame.mybatis.Pagination;
import com.hns.tool.export.ExportUtil;
import com.hns.tool.pub.DateTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.List;

/**
 * 车型信息controlller
 *
 * @author zhangy
 * @version Revision 2.0.0
 * @email: zhangy2222z@sina.cn
 * @create 2017-04-13 19:13
 **/
@Controller
@RequestMapping("/baseCarModelInfo")
public class BaseCarModelInfoController extends BaseController{

    private String[] tableHeader = new String[]{"车辆型号", "备注"};
    private String[] tableFields = new String[]{"carModelName", "remark"};

    @Autowired
    private BaseCarModelInfoService baseCarModelInfoService;

    /**
     * 获取车型分页
     * @param param
     * @return
     * @throws Exception
     */
    @RequestMapping("/getCarModelInfoPage")
    @ResponseBody
    public Object getCarModelInfoPage(BaseCarModelInfoEntity param){
        Pagination page = baseCarModelInfoService.getCarModelInfoPage(param);
        GridPage grid = new GridPage(page);
        return this.getReturnResult(grid);
    }

    /**
     * 导出车型excel
     * @param response
     * @param param
     * @return
     * @throws Exception
     */
    @RequestMapping("/exportCarModelInfoExcel")
    public void exportCarModelInfoExcel(HttpServletResponse response, BaseCarModelInfoEntity param) throws Exception{
        List<BaseCarModelInfoEntity> list = getCarModelInfoList(param);
        ExportUtil.exportFiles(Arrays.asList(tableHeader), Arrays.asList(tableFields), list, response, "excel",
                URLEncoder.encode("驾驶员不安全行为详情", "UTF-8")+ DateTool.getCurrentSysDate("yyyyMMddHHmmss"), "驾驶员不安全行为详情");
    }

    /**
     * 导出车型pdf
     * @param response
     * @param param
     * @return
     * @throws Exception
     */
    @RequestMapping("/exportCarModelInfoPdf")
    public void exportCarModelInfoPdf(HttpServletResponse response, BaseCarModelInfoEntity param) throws Exception{
        List<BaseCarModelInfoEntity> list = getCarModelInfoList(param);
        ExportUtil.exportFiles(Arrays.asList(tableHeader), Arrays.asList(tableFields), list, response, "pdf",
                URLEncoder.encode("驾驶员不安全行为详情", "UTF-8")+ DateTool.getCurrentSysDate("yyyyMMddHHmmss"), "驾驶员不安全行为详情");
    }

    /**
     * 查询导出车型数据
     * @param param
     * @return
     */
    private List<BaseCarModelInfoEntity> getCarModelInfoList(BaseCarModelInfoEntity param){
        List<BaseCarModelInfoEntity> list = baseCarModelInfoService.getCarModelInfoList(param);
        return list;
    }

    /**
     * 添加车型
     * @param param
     * @return
     */
    @RequestMapping("/addCarModelInfo")
    @ResponseBody
    public Object addCarModelInfo(BaseCarModelInfoEntity param) throws Exception{
        String userId = this.getCurrentUser().getUser().getId();
        baseCarModelInfoService.addCarModelInfo(param, userId);
        return this.getReturnResult();
    }

    /**
     * 删除车型
     * @param ids
     * @return
     */
    @RequestMapping("/delCarModelInfo")
    @ResponseBody
    public Object delCarModelInfo(String[] ids){
        baseCarModelInfoService.delCarModelInfo(ids);
        return this.getReturnResult();
    }

    /**
     * 删除车型
//     * @param corpId
     * @return
     */
    @RequestMapping("/getCarModelSelection")
    @ResponseBody
    public Object getCarModelSelection(){
        String corpId = this.getCurrentUser().getUser().getCorpId();
        List list = baseCarModelInfoService.getCarModelSelection(corpId);
        return this.getReturnResult(list);
    }

}
